package qihui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class FSToExcelPOI {

	public static String pathTitle = "C:\\AutomationTest\\v1.4\\v1.4\\openhub fields.txt";
	public static String pathTxt = "C:\\AutomationTest\\v1.4\\v1.4\\1.dat";
	public static String pathExcel = "C:\\AutomationTest\\v1.4\\v1.4\\expected\\openhub.xls";

	public static void main(String args[]) {
		readFileByLines();
	}

	public static void readFileByLines() {
		BufferedReader reader = null;
		BufferedReader readerTitle = null;
		try {
			reader = new BufferedReader(new FileReader(new File(pathTxt)));
			readerTitle = new BufferedReader(new FileReader(new File(pathTitle)));
			String tempString = null;
			WritableWorkbook book = Workbook.createWorkbook(new File(pathExcel));
			WritableSheet sheet = book.createSheet("Test Sheet 1", 0);
			int column = 0;
			while ((tempString = readerTitle.readLine()) != null) {
				Label label = new Label(column, 0, tempString);
				sheet.addCell(label);
				column++;
			}
			int line = 1;
			while ((tempString = reader.readLine()) != null) {
				String[] strTemp = tempString.split("");// 按照斜线拆分成数组
				System.out.println(tempString);
				int length = strTemp.length;
				for (int m = 0; m < length; m++) {
					Label label = new Label(m, line, strTemp[m]);
					sheet.addCell(label);
				}
				line++;
			}
			book.write();
			book.close();
			reader.close();
			readerTitle.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			if (readerTitle != null) {
				try {
					readerTitle.close();
				} catch (IOException e1) {
				}
			}
		}
	}
}
